package cn.starremote.component.utils.encrypt;

import lombok.extern.slf4j.Slf4j;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Security;

/**
 * AesUtils 解密
 * @Author: Wangjw
 */
@Slf4j
public class AesUtils {

    /**
     * 敏感字段AES加密
     * @Author: Wangjw
     * @param str   敏感字段原文
     * @param key   AES密钥.getByte()
     * @return
     */
    public static byte[] Aes256Encode(String str, byte[] key) {
        byte[] result = null;
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "BC");
            SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); // 生成加密解密需要的Key
            cipher.init(Cipher.ENCRYPT_MODE, keySpec);
            result = cipher.doFinal(str.getBytes("UTF-8"));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    /**
     * 敏感字段密文解密
     * @Author: Wangjw
     * @param bytes  要被解密的密文.getByte
     * @param key    AES密钥.getByte
     * @return       解密后的字符串
     */
    public static String Aes256Decode(byte[] bytes, byte[] key) {
        String result = null;
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "BC");
            SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); // 生成加密解密需要的Key
            cipher.init(Cipher.DECRYPT_MODE, keySpec);
            byte[] decoded = cipher.doFinal(bytes);
            result = new String(decoded, "UTF-8");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    /**
     * 敏感字段密文解密--主要用于解密对账明细文件
     * @Author: Wangjw
     * @param bytes  要被解密的密文.getByte
     * @param key    AES密钥.getByte
     * @return  解密后的byte数组
     */
    public static byte[] Aes256DecodeToByte(byte[] bytes, byte[] key) {
        try {
        	Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "BC");
            SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); // 生成加密解密需要的Key
            cipher.init(Cipher.DECRYPT_MODE, keySpec);
            byte[] decoded = cipher.doFinal(bytes);
            return decoded;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
    /**
     *获取用于加密敏感信息的aes密钥
     *@Author: Wangjw
     */
	public static byte[] generateAESKey(String seed) {    
		try {    
	    	KeyGenerator kgen = KeyGenerator.getInstance("AES");
	        SecureRandom securerandom = new SecureRandom(tohash256Deal(seed));
	        kgen.init(256, securerandom);
	        SecretKey secretKey = kgen.generateKey();
	        byte[] enCodeFormat = secretKey.getEncoded();
	        return enCodeFormat;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
	}
	
	/** 
	 * 将指定字符串做hash算法处理
     * @Author: Wangjw
	 * @param datastr  需要被处理的字符串 
	 * @return 
	 */  
	private static byte[] tohash256Deal(String datastr) {    
	    try {    
	        MessageDigest digester=MessageDigest.getInstance("SHA-256");
	        digester.update(datastr.getBytes());   
	        byte[] hex=digester.digest();   
	        return hex;     
	    } catch (NoSuchAlgorithmException e) {    
	        throw new RuntimeException(e.getMessage());      
	    }    
	}
}
